﻿using ERPU8WebApi.Until;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ERPU8WebApi.Models
{
    public class BaseOrderModel
    {
        public BaseOrderModelClass headBase;
        public List<BaseOrderModelClass> bodyBase;
    }
    public class BaseOrderModelClass
    {
    }
    public class BaseOrderModelController
    {
        public virtual BaseOrderModel GetBaseOrderModel(string xmlHead, List<string> xmlBodyList, int bodyCount)
        {
            BaseOrderModel soo = new BaseOrderModel();
            return soo;
        }

        //未修改完
        public BaseOrderModel GetBaseOrderModel(string xmlHead, List<string> xmlBodyList, int bodyCount,BaseOrderModel soo,
            BaseOrderModelClass head,List<BaseOrderModelClass> body)
        {

            xmlHead = xmlHead.Replace("=", "");
            string[] strArray = xmlHead.Split(new string[] { "\"" }, StringSplitOptions.None);
            for (int i = 0; i < strArray.Length; i++)
            {
                if (i % 2 == 0)
                {
                    if (SerializeLibrary.ContainProperty(head, strArray[i]))
                    {
                        if (i + 1 < strArray.Length)
                        {
                            if (head.GetType().GetField(strArray[i]) != null)
                            {
                                head.GetType().GetField(strArray[i]).SetValue(head, strArray[i + 1]);
                            }
                        }
                    }
                }
            }
            for (int n = 0; n < bodyCount; n++)
            {
                FinishedProductsWarehousingBillBodyClass soob = new FinishedProductsWarehousingBillBodyClass();
                xmlBodyList[n] = xmlBodyList[n].Replace("=", "");
                string[] strArrayB = xmlBodyList[n].Split(new string[] { "\"" }, StringSplitOptions.None);
                for (int i = 0; i < strArrayB.Length; i++)
                {
                    if (i % 2 == 0)
                    {
                        if (SerializeLibrary.ContainProperty(soob, strArrayB[i]))
                        {
                            if (i + 1 < strArrayB.Length)
                            {
                                if (soob.GetType().GetField(strArrayB[i]) != null)
                                {
                                    soob.GetType().GetField(strArrayB[i]).SetValue(soob, strArrayB[i + 1]);
                                }
                            }
                        }
                    }
                }

                body.Add(soob);
            }
            soo.headBase = head;
            soo.bodyBase = body;
            return soo;
        }

        //新增或修改时 将对象转化为xml
        public string ReturnXmlHead(BaseOrderModelClass soo, string xmlPath)
        {
            string path = System.AppDomain.CurrentDomain.BaseDirectory + @"XML\" + xmlPath + @"\表头模板.txt";
            string xmlHead = System.IO.File.ReadAllText(path);
            int indexHead = xmlHead.IndexOf("<rs:data>");
            string head = xmlHead.Substring(0, indexHead + 9);
            string center = "<z:row ";
            Dictionary<string, string> dic1 = SerializeLibrary.GetFiledNameValue(soo);
            foreach (var item in dic1)
            {
                center += item.Key + "=" + item.Value + " ";
            }
            center += "/> ";
            int indexBody = xmlHead.IndexOf("</rs:data>");
            string ends = xmlHead.Substring(indexBody, xmlHead.Length - indexBody);
            string xml = head + center + ends;
            return xml;
        }

        public string ReturnXmlBody(List<BaseOrderModelClass> soo, string xmlPath)
        {
            string path = System.AppDomain.CurrentDomain.BaseDirectory + @"XML\" + xmlPath + @"\表体模板.txt";
            string xmlHead = System.IO.File.ReadAllText(path);
            int indexHead = xmlHead.IndexOf("<rs:data>");
            string head = xmlHead.Substring(0, indexHead + 9);
            string centers = "";
            for (int i = 0; i < soo.Count; i++)
            {
                string center = "<z:row ";
                Dictionary<string, string> dic1 = SerializeLibrary.GetFiledNameValue(soo[i]);
                foreach (var item in dic1)
                {
                    center += item.Key + "=" + item.Value + " ";
                }
                center += "/> ";
                centers += center;
            }
            int indexBody = xmlHead.IndexOf("</rs:data>");
            string ends = xmlHead.Substring(indexBody, xmlHead.Length - indexBody);
            string xml = head + centers + ends;
            return xml;
        }

    }


}